package org.batatasframework.common;

import java.text.NumberFormat;

public enum CurrencyRate {
	
	/** 自动转换 */
	AUTO(0, null, null){
		
		@Override
		public String format(double value) {
			if(YI.greaterThan(value)) return YI.format(value);
			if(WAN.greaterThan(value)) return WAN.format(value);
			return BASELINE.format(value);
		}
		
	},
	
	/** 单位为元 */
	BASELINE(1, null, CurrencyMarker.LOWER_FORMATER){
		
		@Override
		public String format(double value) {
			return precision.format(value);
		}
		
	},
	
	/** 单位为万元 */
	WAN(10000, "万元"),
	
	/** 单位为亿元 */
	YI(100000000, "亿元");

	protected final long rate;
	protected final String name;
	protected final NumberFormat precision;
	
	private CurrencyRate(long rate, String name, NumberFormat precision){
		this.rate = rate;
		this.name = name;
		this.precision = precision;
	}
	
	private CurrencyRate(long rate, String name){
		this(rate, name, CurrencyMarker.HIGHER_FORMATER);
	}
	
	public String format(double value){
		return precision.format(value / rate) + name;
	}
	
	private boolean greaterThan(double value){
		return value > rate;
	}
}
